package io.github.yotta.export.core.model;

import io.github.yotta.export.core.exception.DuplicateKeyException;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yotta
 * @date 2023/10/24 16:25
 */
public class ExecuteContext {

    private final Map<String, Object> context = new HashMap<>();


    public void addContext(String key, Object value) {
        if (context.containsKey(key)) {
            throw new DuplicateKeyException(MessageFormat.format("The key {0} is exist", key));
        }
        context.put(key, value);
    }

    public void addContext(String key, Object value, boolean ignoreExist) {
        context.put(key, value);
    }

    public Object getValue(String key) {
        return context.get(key);
    }

    public <T> T getValue(String key, Class<T> clazz) {
        Object value = context.get(key);
        if (value == null) {
            return null;
        }
        return (T) value;
    }


}
